import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyStack() {
        elem = new int[DEFAULT_SIZE];
    }

    //存储数据
    public void push(int val) {
        if(isFUll()) {
            //扩容
            grow();
        }
        elem[usedSize] = val;
        usedSize++;
    }

    private void grow() {
        elem = Arrays.copyOf(elem,elem.length + 5);
    }

    public boolean isFUll() {
        return usedSize == elem.length;
    }

    //删除数据
    public int pop() {
        if(isEmmpty()) {
            throw new StackEmptyException("栈为空异常！");
        }
        usedSize--;
        return elem[usedSize];
    }

    public boolean isEmmpty() {
        return usedSize == 0;
    }

    //获取栈顶元素，但是不删除
    public int peek() {
        if(isEmmpty()) {
            throw new StackEmptyException("栈为空异常！");
        }
        return elem[usedSize--];
    }

    public int size() {
        return usedSize;
    }

}
